package cc.linker.steplink.crm.controller.h5;

import cc.linker.steplink.crm.commons.Constants;
import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.domain.BindUserDto;
import cc.linker.steplink.crm.domain.CustomerBindRelation;
import cc.linker.steplink.crm.domain.WxOrgInfo;
import cc.linker.steplink.crm.merchant.service.NewMallService;
import cc.linker.steplink.crm.merchanth5.commons.RequestFilter;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.service.ICustomerBindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * 加入文章分享或员工绑定
 */
@Controller
@RequestMapping("/personal")
public class CustomerBindController {
    
    @Autowired
    private ICustomerBindService customerBindService;
    @Autowired
    private NewMallService newMallService;
    @Autowired
    private RequestFilter requestFilter;

    /**
     * 加入文章分享
     * @param openId 微信openID
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/join", method = RequestMethod.POST)
    @ResponseBody
    public void join(BindUserDto bindUser, HttpServletRequest request) throws GlobalException {
        transferBindUser(bindUser, request);
        WxUser wxUser =  requestFilter.getWxUser(request);
        String  customerId= wxUser.getCustomerId();
        customerBindService.joinArticleShare(bindUser,customerId);
    }

    /**
     * 绑定成为内部员工
     * @param openid 
     * @param usercode 
     * @param  
     */
    @RequestMapping(value = "/bind", method = RequestMethod.POST)
    @ResponseBody
    public void bind(@RequestBody BindUserDto bindUserDto, HttpServletRequest request) throws GlobalException {
        transferBindUser(bindUserDto, request);
        WxUser wxUser =  requestFilter.getWxUser(request);
        String  customerId= wxUser.getCustomerId();
        customerBindService.bindCustomerToStaff(bindUserDto,customerId);
    }

    /**
     * 取消绑定
     * @param openid
     * @param usercode
     * @param
     */
    @RequestMapping(value = "/unbind", method = RequestMethod.POST)
    @ResponseBody
    public void unbind(BindUserDto bindUserDto, HttpServletRequest request) throws GlobalException {
        transferBindUser(bindUserDto, request);
        WxUser wxUser =  requestFilter.getWxUser(request);
        String  customerId= wxUser.getCustomerId();
        customerBindService.unbindCustomer(bindUserDto,customerId);
    }

    /**
     * 检查绑定状态
     * @param openid
     * @param usercode
     * @param
     */
    @RequestMapping(value = "/checkBind", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> checkBind(@RequestBody BindUserDto bindUserDto, HttpServletRequest request) throws GlobalException,
            IOException, URISyntaxException {
        transferBindUser(bindUserDto, request);
        WxUser wxUser =  requestFilter.getWxUser(request);
        String  customerId= wxUser.getCustomerId();
        Map<String, Object> returnMap = new HashMap<>();
        boolean bindState = customerBindService.checkCustomerBind(bindUserDto,customerId);
        returnMap.put("bindState", bindState);
        if (CustomerBindRelation.StaffType.INNER_STAFF.getType().equals(bindUserDto.getType())) {
            // 绑定类型为内部员工获取机构信息
            WxOrgInfo orgInfo = customerBindService.getOrgInfo(bindUserDto);
            if (bindState) {
                if (orgInfo == null) {
                    orgInfo = new WxOrgInfo();
                }
                orgInfo.setUsercode(customerBindService.getUsercode(bindUserDto));
            }
            returnMap.put("orgInfo", orgInfo);
        }
        return returnMap;
    }

    /**
     * 永久二维码
     *
     * @param goodsId 商品id
     * @param flag    是否大b,true-大b,false小b
     */
    @GetMapping("/qrCode")
    public String qrCode(String goodsId, Integer goodsType, boolean flag,String token) {
        return newMallService.qrCode(goodsId, goodsType, flag,token);
    }

    /**
     * 获取登录用户信息
     * @param bindUserDto
     * @param request
     */
    private void transferBindUser(BindUserDto bindUserDto, HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId= wxUser.getCustomerId();
        Map map = customerBindService.getOpenIdAndToken(customerId);
        String openId = (String) map.get("openId");
        String token = (String) map.get("token");
        wxUser.setOpenid(openId);
        wxUser.setToken(token);
        if (bindUserDto == null) {
            bindUserDto = new BindUserDto(wxUser);
        } else {
            bindUserDto.setWxInfo(wxUser);
        }
    }
}
